﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;

namespace 智慧无纸化会议客户端软件
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                Process currentProcess = Process.GetCurrentProcess();
                if (Process.GetProcessesByName(currentProcess.ProcessName).Length > 1 && !JudgeReStart())
                {
                    MessageBox.Show("应用程序已经在运行中...");
                    Environment.Exit(0);

                    return;
                }

            }
            catch
            {
            }

            base.OnStartup(e);
        }


        public static string InitDirPath(string dirPath)
        {
            if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
            return dirPath;
        }

        bool JudgeReStart()
        {
            InitDirPath(string.Format("{0}Data", AppDomain.CurrentDomain.BaseDirectory));
            string path = string.Format("{0}Data\\ReStart.txt", AppDomain.CurrentDomain.BaseDirectory);
            string content = File.ReadAllText(path);

            File.WriteAllText(path, string.Empty);

            return content == "1";
        }

        protected override void OnExit(ExitEventArgs e)
        {
            try
            {
                if (EventAction.ExitAction != null) EventAction.ExitAction();
                base.OnExit(e);
            }
            catch
            {
            }
        }
    }
}
